import shutil
import subprocess
import re

# 检查待发布包的版本号
with open('pyproject.toml', 'r', encoding='utf-8') as file:
    line = file.readline()
    while line:
        if "version" in line:
            local_version_str = line
        line = file.readline()
# 匹配双引号之间的内容
local_version = re.findall(r'"([^"]*)"', local_version_str)

result = subprocess.run('pip index versions tfc_toolbox_py',capture_output=True, text=True)

package_version_str = result.stdout
# 匹配括号之间的内容
package_version = re.findall(r'\((.*?)\)', package_version_str)

print(f"待发布包的版本：{local_version[0]}")
print(f"在线版本：{package_version[0]}") # 标准输出

if local_version[0] != package_version[0]:
    print("版本号检测通过")

    # 删除构建文件夹
    shutil.rmtree("dist")
    # 构建包
    subprocess.run('py -m build')
    # 发布包
    subprocess.run('py -m twine upload --repository pypi dist/*')
else:
    print("版本号检测未通过，请修改版本号")